package app

import (
	"server/app/common/request"
	"server/app/common/response"
	"server/app/services"
	"server/global"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

/* 视频列表 */
func VideoList(ctx *gin.Context) {
	var form request.VideoList
	response.ParsePagination(ctx, &form.Page) // 使用封装的函数设置分页参数

	if err := ctx.ShouldBindQuery(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}

	/*
		没走auth
		if userId, ok := ctx.Keys["id"].(string); ok {
			intId, err := strconv.Atoi(userId)
			if err != nil {
				log.Println("userID校验失败")
			} else {
				form.UserID = intId
			}

		}
	*/

	if err, list := services.VideoService.VideoList(form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, list)
	}
}

/* 点赞 */
func VideoLike(ctx *gin.Context) {
	var form request.VideoLike

	userId, ok := ctx.Keys["id"]
	if !ok || userId == nil {
		response.BusinessFail(ctx, "登录凭证过期或不存在")
		return
	}
	form.UserId = userId.(int)

	if err := ctx.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	if err := services.VideoService.Like(form); err != nil {
		global.App.Log.Error("app/app.go:VideoLike() err:%v", zap.Any("err", err))
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, nil)
	}
}

/* 发送评论 */
/* RESTful api */
func VideoComment(ctx *gin.Context) {
	var form request.VideoComment
	videoIdStr := ctx.Param("videoId")
	videoId, err := strconv.Atoi(videoIdStr)
	if err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	form.VideoId = videoId

	if err := ctx.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	if err := services.VideoService.Comment(form); err != nil {
		global.App.Log.Error("app/app.go:VideoComment() err:%v", zap.Any("err", err))
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, nil)
	}
}

/* 评论列表 */
func VideoCommentList(ctx *gin.Context) {
	var form request.VideoCommentList
	videoIdStr := ctx.Param("videoId")
	videoId, err := strconv.Atoi(videoIdStr)
	if err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	form.VideoId = videoId
	response.ParsePagination(ctx, &form.Page)

	if err := ctx.ShouldBindQuery(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	if err, list := services.VideoService.CommentList(form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, list)
	}
}

/* 视频详情 */
func VideoDetail(ctx *gin.Context) {
	var form request.VideoDetail
	videoIdStr := ctx.Param("videoId")
	videoId, err := strconv.Atoi(videoIdStr)
	if err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	form.VideoId = videoId

	if err := ctx.ShouldBindQuery(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	if err, result := services.VideoService.VideoDetail(form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, result)
	}
}

/* 删除视频:（未做软删除） */
func DeleteVideo(ctx *gin.Context) {
	var form request.DeleteVideo
	videoIdStr := ctx.Param("videoId")
	videoId, err := strconv.Atoi(videoIdStr)
	if err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	form.VideoId = videoId

	userIdStr, ok := ctx.Keys["id"].(string)
	if !ok {
		response.BusinessFail(ctx, "用户身份校验失败，请检查token")
		return
	}

	// 将字符串形式的用户ID转换为整数
	userId, err := strconv.Atoi(userIdStr)
	if err != nil {
		response.BusinessFail(ctx, "用户ID格式错误")
		return
	}
	form.UserId = userId

	if err := ctx.ShouldBindQuery(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}
	if err := services.VideoService.DeleteVideo(form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, nil)
	}
}

/* 更新视频信息 */
func UpdateVideo(ctx *gin.Context) {
	var form request.UpdateVideo
	videoIdStr := ctx.Param("videoId")
	videoId, err := strconv.Atoi(videoIdStr)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	form.VideoId = videoId
	userId := ctx.Keys["id"].(string)

	if err := services.VideoService.UpdateVideo(userId, form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, nil)
	}
}
